package org.dftproject.lineagelinkage.ui.pages;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import org.dftproject.genesis.ui.figures.TooltipLabel;
import org.dftproject.genesis.ui.figures.dualtree.ISelectableFigure;
import org.dftproject.genesis.ui.pages.pedigree.tree.AlphaFigure;
import org.dftproject.lineagelinkage.ui.Activator;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.ToolbarLayout;

public class MergeNode extends AlphaFigure implements ISelectableFigure {

	public static final int STATE_UNKNOWN = 0;
	public static final int STATE_SAME = 1;
	public static final int STATE_DIFFERENT = 2;
	
	private boolean selected = false;

	private int state = STATE_UNKNOWN;

	private final PersonData person;
	private final PersonData duplicate;
	
	private final Figure personFigures;
	private PersonFigure primaryFigure;
	private PersonFigure duplicateFigure;

	public MergeNode(PersonData person, PersonData duplicate) {
		this.person = person;
		this.duplicate = duplicate;
		
		ToolbarLayout layout = new ToolbarLayout(false);
		layout.setMinorAlignment(ToolbarLayout.ALIGN_BOTTOMRIGHT);
		layout.setSpacing(5);
		setLayoutManager(layout);
		
		if (person != null && duplicate != null) {
			add(new RatingFigure(5));
		}
		
		Figure bottom = new Figure();
		layout = new ToolbarLayout(true);
		layout.setSpacing(5);
		bottom.setLayoutManager(layout);
		add(bottom);
		
		personFigures = new Figure();
		layout = new ToolbarLayout(false);
		layout.setSpacing(5);
		personFigures.setLayoutManager(layout);
		bottom.add(personFigures);
		
		if (person != null) {
			primaryFigure = new PersonFigure(person, false);
			primaryFigure.addPropertyChangeListener(ISelectableFigure.SELECTION_PROPERTY, new PropertyChangeListener() {

				public void propertyChange(PropertyChangeEvent event) {
					if (Boolean.TRUE.equals(event.getNewValue())) {
						setSelected(true);
					}
				}

			});
			personFigures.add(primaryFigure);
		}
		if (duplicate != null) {
			duplicateFigure = new PersonFigure(duplicate, true);
			duplicateFigure.addPropertyChangeListener(ISelectableFigure.SELECTION_PROPERTY, new PropertyChangeListener() {

				public void propertyChange(PropertyChangeEvent event) {
					if (Boolean.TRUE.equals(event.getNewValue())) {
						setSelected(true);
					}
				}

			});
			personFigures.add(duplicateFigure);
		}
		
		if (person != null && duplicate != null) {
			Figure buttons = new Figure();
			layout = new ToolbarLayout(false);
			layout.setSpacing(2);
			buttons.setLayoutManager(layout);
			buttons.setBorder(new MarginBorder(0, 0, 0, 5));
			bottom.add(buttons);

			final TwoStateButtonFigure sameBtn = new TwoStateButtonFigure(Activator.getImageDescriptor("icons/tick.png").createImage());
			final TwoStateButtonFigure differentBtn = new TwoStateButtonFigure(Activator.getImageDescriptor("icons/cross.png").createImage());
			
			sameBtn.setToolTip(new TooltipLabel("Merge these two individuals"));
			sameBtn.addPropertyChangeListener(ISelectableFigure.SELECTION_PROPERTY, new PropertyChangeListener() {

				public void propertyChange(PropertyChangeEvent event) {
					if (Boolean.TRUE.equals(event.getNewValue())) {
						setState(STATE_SAME);
						differentBtn.setSelected(false);
					} else if (!differentBtn.isSelected()) {
						setState(STATE_UNKNOWN);
					}
				}

			});
			buttons.add(sameBtn);
			
			differentBtn.setToolTip(new TooltipLabel("These two individuals are not the same"));
			differentBtn.addPropertyChangeListener(ISelectableFigure.SELECTION_PROPERTY, new PropertyChangeListener() {

				public void propertyChange(PropertyChangeEvent event) {
					if (Boolean.TRUE.equals(event.getNewValue())) {
						setState(STATE_DIFFERENT);
						sameBtn.setSelected(false);
					} else if (!sameBtn.isSelected()) {
						setState(STATE_UNKNOWN);
					}
				}

			});
			buttons.add(differentBtn);
		}
	}

	public void setSelected(boolean selected) {
		if (this.selected == selected)
			return;

		this.firePropertyChange(ISelectableFigure.SELECTION_PROPERTY, this.selected, selected);
		this.selected = selected;
		
		if (primaryFigure != null)
			primaryFigure.setSelected(selected);
		if (duplicateFigure != null)
			duplicateFigure.setSelected(selected);

		repaint();
	}

	public boolean isSelected() {
		return selected;
	}
	
	public void setState(int state) {
		this.state = state;
	}
	
	public int getState() {
		return state;
	}
	
}
